home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / progjour / 1989 / 05 / asmhdr.c next >
C/C++ Source or Header  |  1989-06-27  |  4KB  |  145 lines

  1. /*  asmhdr - process assembly language headers
  2.  
  3.     usage:  asmhdr document_file *.asm
  4.  
  5.     Borland   C>tcc -mc asmhdr.c wildargs.obj
  6.  
  7.     You may have to specify correct path information
  8.     for the wildargs obj files.  Compile with compact
  9.     memory model.  This program was written for Turbo
  10.     C but it would be easy to modify for Microsoft C.
  11.  
  12.     John Otken, Soft Advances
  13. */
  14.  
  15. #include <alloc.h>      /* use malloc.h for microsoft c */
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19.  
  20. #define LINE_SIZE       256
  21. #define MAX_ROUTINES    1000    /* maximum routines */
  22. #define READ_TEXT       "rt"
  23. #define WRITE_TEXT      "wt"
  24.  
  25.  
  26. /* prototypes begin */
  27. void process_asm_file(char *fn);
  28. void out_of_memory(void);
  29. int stricmp_pt(struct index_tag *a, struct index_tag *b);
  30. /* prototypes end */
  31.  
  32.  
  33. struct index_tag {
  34.     char *routine_name;
  35.     char *routine_header;
  36. } index[MAX_ROUTINES];
  37.  
  38.  
  39. int index_cnt = 0;              /* number of index entries */
  40. unsigned long coreused = 0;     /* total storage used */
  41.  
  42.  
  43. void main(int argc, char *argv[])
  44. {
  45.     int i;
  46.     FILE *fo;
  47.  
  48.     if (argc < 3) {
  49.         printf("asmhdr outfile *.asm");
  50.         exit(0);
  51.     }
  52.  
  53.     i = 1;
  54.     fo = fopen(argv[i], READ_TEXT);
  55.     if (fo != NULL) {
  56.         printf("Error: %s exists!  Overwriting source file?\n", argv[i]);
  57.         fclose(fo);
  58.         exit(1);
  59.     }
  60.  
  61.     fo = fopen(argv[i], WRITE_TEXT);
  62.     if (fo == NULL) {
  63.         printf("Error opening output file: %s\n", argv[i]);
  64.         exit(1);
  65.     }
  66.  
  67.     for(i = 2; i < argc; i++)
  68.         process_asm_file(argv[i]);
  69.  
  70.     qsort(&index[0], index_cnt, sizeof(index[0]), stricmp_pt);
  71.  
  72.     for(i = 0; i < index_cnt; i++)
  73.         fprintf(fo,"%s\n",index[i].routine_header);
  74.     fclose(fo);
  75.  
  76.     printf("Used %d of %d index entries.\n", index_cnt, MAX_ROUTINES);
  77.     printf("Used %lu bytes.\n", coreused);
  78.                   /* microsoft c does not have a coreleft function */
  79.     printf("%lu free bytes.\n", (unsigned long) coreleft());
  80. }
  81.  
  82.  
  83.  
  84.  
  85. void process_asm_file(char *fn)
  86. {
  87.     char *p, *q;
  88.  
  89.     char line[LINE_SIZE],   /* file line buffer */
  90.          buf[3000];         /* temp buffer */
  91.  
  92.     FILE *fi;
  93.  
  94.     fi = fopen(fn, READ_TEXT);
  95.     if (fi == NULL) {
  96.         printf("Error opening input file: %s\n", fn);
  97.         exit(1);
  98.     }
  99.  
  100.     while (fgets(line, LINE_SIZE, fi) != NULL) {
  101.         if (line[0] == ';'  &&  line[1] == ';'  &&  line[2] == '\t') {
  102.  
  103.             p = buf;
  104.             do {
  105.                 p = stpcpy(p, line);
  106.             } while (fgets(line, LINE_SIZE, fi) != NULL  &&  line[0] == ';');
  107.             if (*(p-1) == '\n'  &&  *(p-2) == ';')  p -= 2;
  108.             *p = '\0';
  109.             if ((index[index_cnt].routine_header = strdup(buf)) == NULL)
  110.                 out_of_memory();
  111.             coreused += strlen(buf)+1;
  112.  
  113.             p = &buf[3];        /* extract routine name from header */
  114.             q = buf;
  115.             while (*p != '\n') {
  116.                 if (*p == ' ')  *p = '_';
  117.                 *q++ = *p++;
  118.             }
  119.             *q = '\0';
  120.  
  121.             if ((index[index_cnt++].routine_name = strdup(buf)) == NULL)
  122.                 out_of_memory();
  123.             coreused += strlen(buf)+1;
  124.         }
  125.     }
  126.     fclose(fi);
  127. }
  128.  
  129.  
  130.  
  131.  
  132. void out_of_memory(void)
  133. {
  134.     printf("Out of memory");
  135.     exit(1);
  136. }
  137.  
  138.  
  139.  
  140.  
  141. int stricmp_pt(struct index_tag *a, struct index_tag *b)
  142. {
  143.     return stricmp(a->routine_name, b->routine_name);
  144. }
  145.